package leetcode.code0630;

import java.util.PriorityQueue;

import leetcode.helper.HelpDebug;

public class Solution extends S630 {

	@Override
	public int scheduleCourse(int[][] courses) {
		PriorityQueue<int[]> p = new PriorityQueue<>((a, b) -> {
//			if (a[0] != b[0]) {
//				return a[0] - b[0];
//			} else {
//				return a[1] - b[1];
//			}
			if (a[1] != b[1]) {
				return a[1] - b[1];
			} else {
				return a[0] - b[0];
			}
		});
		int ans = 0;
		for (int[] element : courses) {
			int durationi = element[0];
			int lastDayi = element[1];
			if (lastDayi > durationi) {
				p.add(new int[] { lastDayi - durationi, durationi, lastDayi });
			}
		}
		int start = 0;
		while (!p.isEmpty()) {
			int[] course = p.poll();
			HelpDebug.print(course);
			if (start <= course[0]) {
//				System.out.print("  do" );
//				System.out.println();
				start += course[1];
				ans += 1;
			}
		}
		return ans;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug5();
//		so.debug1();
//		so.debug2();
//		so.debug3();

	}

}
